#include <iostream>
#include <signal.h>
#include <unistd.h>

void PrintBlock()
{
    sigset_t set, oset;
    sigemptyset(&set);
    sigemptyset(&oset);

    sigprocmask(SIG_BLOCK, &set, &oset);
    std::cout << "block :";
    for(int signo = 31; signo > 0; signo--)
    {
        if(sigismember(&oset, signo))
        {
            std::cout << 1;
        }
        else 
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}

void PrintPending()
{
    sigset_t pending;
    ::sigpending(&pending);

    std::cout << "Pending :";
    for(int signo = 31; signo > 0; signo--)
    {
        if(sigismember(&pending, signo))
        {
            std::cout << 1;
        }
        else 
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}

void handler(int signo)
{
    static int cnt = 0;
    cnt++;
    while (true)
    {
        std::cout << "get a sig" << signo << ", cnt: " << cnt << std::endl;
        PrintBlock();
        ::sleep(1);
        break;
    }
}

int main()
{
    struct sigaction act, oact;
    act.sa_handler = handler;
    sigemptyset(&act.sa_mask);
    sigaddset(&act.sa_mask, 3);
    sigaddset(&act.sa_mask, 4);
    sigaddset(&act.sa_mask, 5);
    sigaddset(&act.sa_mask, 6);
    sigaddset(&act.sa_mask, 7);

    ::sigaction(2, &act, &oact);

    while (true)
    {
        // PrintBlock();
        PrintPending();
        ::pause();
    }

    return 0;
}